<template>
  <div ref="imageWrapper">
    <a-card title="查询条件" class="search">
      <div class="table-page-search-wrapper">
        <a-form-model layout="inline" ref="ruleForm" :model="queryParam" :rules="rules" @keyup.enter.native="onSearch">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-model-item label="单位名称" prop="unitId">
                <j-select-depart
                  v-model="queryParam.unitId"
                  :trigger-change="true"
                  customReturnField="id"
                  :multi="true"
                  style="width: 100%"
                ></j-select-depart>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-model-item label="年度" prop="yearNum">
                <a-date-picker
                  mode="year"
                  placeholder="请选择年份"
                  format="YYYY"
                  value-format="yyyy-MM-dd"
                  v-model="queryParam.yearNum"
                  :open="yearPickShow"
                  style="width: 100%"
                  @panelChange="handlePanelChange"
                  @openChange="handleOpenChange"
                />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8" :offset="6">
              <span style="float: right; overflow: hidden" class="table-page-search-submitButtons">
                <a-button type="primary" @click="onSearch" icon="search" class="yellow_search_btn">查询</a-button>
                <a-button @click="searchReset" icon="reload" class="clear_btn">清除条件</a-button>
                <a-button
                  v-if="activeKey == '0'"
                  type="danger"
                  :loading="ydLoading"
                  @click="handleToYD()"
                  icon="arrow-right"
                  style="margin-left: 18px"
                  >转疑点</a-button
                >
                <a-button v-if="isBackButton" type="primary" style="margin-left: 8px" @click="backUppage">
                  返回
                </a-button>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card style="margin-top: 20px">
      <template slot="title"><span style="font-weight: bold">企业基本情况</span></template>
      <a-tabs
        default-active-key="0"
        @change="handleRequst"
        v-model="activeKey"
        type="card"
        class="tabs_style"
        style="margin-top: 5px"
      >
        <a-tab-pane key="0" tab="公司基本情况指标">
          <a-table
            ref="table"
            size="middle"
            row-key="id"
            :columns="columns3"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
            @change="handleTableChange"
            :row-selection="rowSelection"
            class="antableClass"
          >
          </a-table>
        </a-tab-pane>
        <a-tab-pane key="1" tab="新设子、分公司" force-render>
          <a-table
            ref="table"
            size="middle"
            class="antableClass"
            rowKey="id"
            :columns="columns2"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            @change="handleTableChange"
          >
          </a-table>
        </a-tab-pane>
        <a-tab-pane key="2" tab="公司基本情况指标1">
          <a-table
            ref="table"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
            @change="handleTableChange"
            class="antableClass"
          >
            <template slot="info" slot-scope="text, record">
              <a @click="ViewInfor(record, '', queryParam)">{{ text }}</a>
            </template>
            <template slot="zgs" slot-scope="text, record">
              <a @click="ViewInfor(record, '1', queryParam)">{{ text }}</a>
            </template>
            <template slot="fgs" slot-scope="text, record">
              <a @click="ViewInfor(record, '2', queryParam)">{{ text }}</a>
            </template>
            <template slot="yzxgs" slot-scope="text, record">
              <a @click="ViewInfor(record, '3', queryParam)">{{ text }}</a>
            </template>
          </a-table>
        </a-tab-pane>
      </a-tabs>
    </a-card>
    <EnterprisesThree ref="enterprisesthree" />
    <YDModel ref="YDModel" :routerData="$route.path" />
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { ModelMixins } from '../../mixins/ModelMixins'
import EnterprisesThree from './EnterprisesThree.vue'
import { httpAction, getAction, postAction, deleteAction } from '@/api/manage'
import columns from './columns'
import columns2 from './columns2'
import columns3 from './columnsDetails'
import { base64toImg } from '@/mixins/base64toImg'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'
import YDModel from '@/views/sj/components/YDModel'
export default {
  mixins: [JeecgListMixin, base64toImg, ModelMixins, jumpConfigMixin],
  components: { EnterprisesThree, YDModel },
  data() {
    return {
      queryParam: {
        unitId: '',
        yearNum: ''
      },
      superParam: {
        id: 'ab20719669f29b89e8d3b14020b75a01',
        modelId: 'ab20719669f29b89e8d3b14020b75a01'
      },
      isBackButton: false,
      columns: columns(this),
      columns2: columns2(),
      columns3: columns3(),
      dataSource: [],
      dataSource2: [],
      url: {
        // list:'/sjmx/queryQyjbqkXs',
        list: '/sjmx/queryQyjbqkXsXq',
        // 查询模型转疑点信息(多选)
        TurnSuspects: '/sjmx/queryModelMsgMulti'
        // list:'/sjmx/queryQyjbqkXsXq',
      },
      list: ['/sjmx/queryQyjbqkXsXq', '/sjmx/queryQyjbqkXs', '/sjmx/queryQyjbqk3j'],
      disableMixinCreated: true,
      activeKey: '0',
      rules: {
        unitId: [{ required: true, message: '请选择单位名称', trigger: 'change' }],
        yearNum: [{ required: true, message: '请选择年度', trigger: 'change' }]
      },
      selectedRows: [],
      selectedRowKeys: []
    }
  },
  computed: {
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          console.log(selectedRowKeys, selectedRows)
          this.selectedRows = selectedRows
          this.selectedRowKeys = [...selectedRowKeys]
        },
        selectedRowKeys: this.selectedRowKeys,
        getCheckboxProps: record => {
          return {
            props: {
              disabled: !record.id
            }
          }
        }
      }
    }
  },
  watch: {
    $route: {
      handler: function(to) {
        if (Object.keys(to.query).length) {
          debugger
          this.disableMixinCreated = true
          let { activeKey, selectedRowKeys, unitId, yearNum } = to.query
          this.activeKey = activeKey
          this.selectedRowKeys = selectedRowKeys.split(',')
          this.queryParam = Object.assign(this.queryParam, { unitId, yearNum })
          this.url.list = this.list[Number.parseInt(this.activeKey)]
          this.$nextTick(() => {
            this.onSearch()
          })
        } else if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          let { endDate, startDate } = JSON.parse(to.params.returnRecord)
          let { departId, accName, returnRecord } = to.params
          this.queryParam.unitId = departId
          this.queryParam.yearNum = startDate && startDate.split('-')[0]

          this.isBackButton = true
          this.returnRecord = to.params.returnRecord

          this.$nextTick(() => {
            // 这可能存在问题
            this.onSearch()
          })
        }
      },
      immediate: true
    }
  },
  methods: {
    handleRequst(key) {
      console.log(key)
      this.url.list = this.list[Number.parseInt(key)]
      console.log(this.url.list)
      this.onSearch()
    },
    onSearch() {
      this.$refs.ruleForm.validate(value => {
        if (value) {
          this.loadData(1)
        }
      })
    },
    searchReset() {
      this.queryParam = {
        unitId: '',
        yearNum: ''
      }
      this.selectedRows = []
      this.selectedRowKeys = []
      this.dataSource = []
      this.dataSource2 = []
    },

    ViewInfor(record, type, queryParam) {
      this.$refs.enterprisesthree.edit(record, type, queryParam)
    },
    handleToYD() {
      if (this.dataSource.length) {
        const { startTime = '', endTime = '' } = this.queryParam
        // let ydnr = `中铁XX局XX项目未取得增值税劳务发票X亿元，设计税金X万元；X公司城际联络线`
        let param = Object.assign({}, this.queryParam, {
          selectedRowKeys: this.selectedRowKeys.join(','),
          activeKey: this.activeKey
        })
        let ydnr = undefined
        let gzzd = undefined
        const time = `【企业基本情况】`
        let ydList = JSON.stringify(this.selectedRows)
        postAction(this.url.TurnSuspects, Object.assign({}, this.queryParam, this.superParam, { ydList: ydList })).then(
          res => {
            if (res.success) {
              if (res.result.length) {
                ydnr = res.result[0].ydms ? res.result[0].ydms : ydnr
                gzzd = res.result[0].gzzd ? res.result[0].gzzd : gzzd
              }
              this.addYdMulti(param, ydnr, time, gzzd)
            } else {
              this.$message.error(res.message)
            }
            if (res.code === 510) {
              this.$message.warning(res.message)
            }
          }
        )
      } else {
        this.$message.error('空数据不能新建疑点')
      }
    }
  }
}
</script>

<style lang="less" scoped>
@import '~@assets/less/tableTheme.less';

/deep/ .ant-card-body {
  padding: 0px 22px 20px 22px;
}
</style>
